<?php
/**
 *  CMS 子类管理
 *  郎峰 2011-5-12
 */
class Admin_Page_Cms_SubClass extends Admin_Page_Abstract
{
    public function validate(ME_Request $input, ME_Response $output)
    {
        $input->doJump = true;
        $adminInfo = Libs_UcUser::getAdminLoginInfo($input,$output);
        return true;
    }

    private $db;
    private $tblName = 'cms_subclass';
    private function loadDb()
    {
        $this->db = Db_Admin::instance();
    }


    public function doDefault(ME_Request $input, ME_Response $output)
    {
        $page = (int)$input->get("page");
        $page = max(1,$page);
        $this->loadDb();
        //获得所有记录
        $param = array(
                'db'          => $this->db,
                'fromSql'     => " from {$this->tblName} ", #from部分的语句
                'orderBySql'  => ' ORDER BY id ASC ',
                'pagesize'  => 20,
                'page'      => $page,
                'whereSql'  => " WHERE del='0'",
                'urlTpl'    => 'index.php?c=Cms_SubClass&page={PAGE}',
        );
        $listData = Libs_DBHelper::getList($param);
        $output->pageBar = $listData['pageBar'];
        $output->list = $listData['list'];
        #顶部
        $output->headerHtml = Admin_Plugin_Common_Tpl::getHeader($input, $output);
        //频道缓存
        //$output->appCache = $this->loadCache("Global_App",array());
        $output->classCache = array(123 => array('name' => 'sex频道'));
        $output->setTemplate("Cms/SubClassList");
    }
    
    public function doAdd(ME_Request $input, ME_Response $output)
    {
        $this->loadDb();
        $classId    = (int)$input->post("classId");
        $name       = $input->post("name");
        $path       = $input->post("path");
        #拆分子类名和路径
        $nameArr    = explode("\r\n", $name);
        $pathArr    = explode("\r\n", $path);

        $item['classId']    = $classId;
        foreach($nameArr as $key => $value) {
            $item['name']   = $value;
            $item['path']   = $pathArr[$key];
            $options = array(
                    'db'        => $this->db,
                    'tableName' => $this->tblName,
                    'addItem'   => $item,
                    'debug'     => 1,
                );
            Libs_DBHelper::insertItem($options);
        }
        Libs_GlobalFunc::haltMsg("添加子类成功！");
        exit;
    }
    public function doEdit(ME_Request $input, ME_Response $output)
    {
        $this->loadDb();
        $subClassIdArr = $input->post("subClassId");
        if(!$subClassIdArr) Libs_GlobalFunc::haltMsg("请选择一项进行操作！");
        $edit       = $input->post("edit");
        $del        = $input->post("del");
        $classId    = $input->post("classId");
        $name       = $input->post("name");
        $path       = $input->post("path");

        foreach($subClassIdArr as $key => $subClassId) {
            if($edit) {
                $item['classId']    = $classId[$subClassId];
                $item['name']       = $name[$subClassId];
                $item['path']       = $path[$subClassId];
                $str                = '修改';
            } elseif($del) {
                $item['del']        = 1;
                $str                = '删除';
            }
            $options = array(
                    'db'        => $this->db,
                    'tableName' => $this->tblName,
                    'addItem'   => $item,
                    'where'     => " where id = '{$subClassId}'",
                    'debug'     => 1,
                );
            Libs_DBHelper::updateItem($options);
        }
        Libs_GlobalFunc::haltMsg($str . "子类成功！");
        exit;
    }

}
